package org.qhhc.bionioaio.bio;

import org.qhhc.bionioaio.bio.Client.SwingChatClient;
import org.qhhc.bionioaio.bio.Servier.SwingChatServer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 启动器类 - 允许用户选择启动服务器或客户端
 */
public class RunChatApp extends JFrame {
    
    public RunChatApp() {
        // 设置窗口
        setTitle("聊天应用启动器");
        setSize(300, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);  // 居中显示
        
        // 创建按钮
        JButton serverButton = new JButton("启动服务器");
        JButton clientButton = new JButton("启动客户端");
        
        // 设置布局
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2, 1, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 添加按钮到面板
        panel.add(serverButton);
        panel.add(clientButton);
        
        // 添加面板到窗口
        getContentPane().add(panel);
        
        // 添加按钮事件
        serverButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        new SwingChatServer().setVisible(true);
                    }
                });
            }
        });
        
        clientButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        new SwingChatClient().setVisible(true);
                    }
                });
            }
        });
    }
    
    public static void main(String[] args) {
        // 设置外观和感觉
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        // 启动应用
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                RunChatApp launcher = new RunChatApp();
                launcher.setVisible(true);
            }
        });
    }
} 